<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="searchBox searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="accountForm">
          <el-col :lg="8" :xl="6">
            <el-form-item label="模板名称" class="row-padding-bottom">
              <el-input
                placeholder="请输入模板名称"
                clearable
                v-model="filterField.name"
                @blur="filterField.name = changeValue($event)"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="巡查类型" class="row-padding-bottom">
              <el-select
                v-model="filterField.inspectionType"
                placeholder="请选择"
                clearable
              >
                <el-option label="日常巡查" value="DAILY"></el-option>
                <el-option label="专项巡查" value="SPECIAL"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="模板类别" class="row-padding-bottom">
                <el-select
                  v-model="filterField.type"
                  placeholder="请选择"
                  clearable
                >
                  <el-option label="非建领域" value="NC"></el-option>
                  <el-option label="建设领域" value="CS"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!-- <el-form-item>
                        <el-button type="primary" icon="icon iconfont iconchazhao1" @click="searchTable" class="commonBtn"></el-button>
                    </el-form-item> -->
          </template>
          <el-form-item label="">
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchTable"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
            <!-- <exportBtn :dataLen="tableData.length" :filterField="filterField" :api="api"></exportBtn>
                             <el-button class="commonBut saveBtn funComBut" icon="iconfont icontianjia" @click="toAdd()">
                                <span class="opetareText">新建模板</span>
                            </el-button> -->
          </el-form-item>

          <!-- <el-form-item>
                        <el-button class="commonBut saveBtn funComBut" icon="iconfont icontianjia" @click="toAdd()">
                            <span class="opetareText">新建模板</span>
                        </el-button>
                    </el-form-item> -->
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <!-- 列表 -->
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <div style="margin-bottom: 24px">
          <exportBtn
            :dataLen="tableData.length"
            :filterField="filterField"
            :api="api"
          ></exportBtn>
          <el-button
            class="commonBut saveBtn funComBut"
            icon="iconfont icontianjia"
            @click="toAdd()"
          >
            <span class="opetareText">新建模板</span>
          </el-button>
        </div>
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableData"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          @sort-change="sortChange"
          stripe
          v-loading="loading"
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="inspectionTypeDesc"
            label="巡查类型"
            :formatter="formatTd"
            min-width="200"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="typeDesc"
            label="模板类别"
            :formatter="formatTd"
            min-width="200"
          ></el-table-column>
          <!-- <el-table-column align="center" prop="name" label="模板名称" :formatter=formatTd></el-table-column> -->
          <el-table-column label="模板名称" min-width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.name"
                :columnName="'name'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="itemCount"
            label="巡查事项数"
            sortable="custom"
            :formatter="formatTd"
            min-width="200"
          ></el-table-column>
          <el-table-column align="left" label="操作" width="210" fixed="right">
            <template slot-scope="scope">
              <el-link type="primary" @click="toDetail(scope.row)"
                >模板详情</el-link
              >
              <el-link type="primary" @click="toEdit(scope.row)">编辑</el-link>
              <el-link type="primary" @click="toDel(scope.row)">删除</el-link>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'patrolTemplateList',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '巡查模板管理',
      advanced: false,
      requestUrl: 'api-gov/ipqc/template/expand/list',
      loading: true,
      isShow: false,
      tableData: [], //表格数据。
      filterField: {
        name: '',
        inspectionType: '',
        type: '',
      },
      pageSize: 20,
      pageNo: 1,
      advanced: false,
      api: '',
    }
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.getApi()
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableData = data
      this.loading = false
      if (this.tableData.length < 1) {
        this.isShow = true
      }
    },
    //列表查询
    searchTable() {
      this.pageSize = 20
      this.pageNo = 1
      this.$refs.pagination.getTableListData()
    },
    toAdd() {
      this.$router.push({
        path: '/home/patrolTemplate/patrolTemplateAdd',
        name: 'patrolTemplateAdd',
        query: {
          edit: 'Add',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/patrolTemplate/patrolTemplateList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    toDetail(row) {
      if (row.type == 'CS') {
        this.$router.push({
          path: '/home/patrolTemplate/patrolTemplateDetailJz',
          name: 'patrolTemplateDetailJz',
          query: {
            page: 'patrolTemplateList',
            templateId: row.id,
          },
        })
      }
      if (row.type == 'NC') {
        this.$router.push({
          path: '/home/patrolTemplate/patrolTemplateDetailFj',
          name: 'patrolTemplateDetailFj',
          query: {
            page: 'patrolTemplateList',
            templateId: row.id,
          },
        })
      }
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/patrolTemplate/patrolTemplateList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    toEdit(row) {
      this.$router.push({
        path: '/home/patrolTemplate/patrolTemplateAdd',
        name: 'patrolTemplateAdd',
        query: {
          edit: 'Update',
          templateId: row.id,
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/patrolTemplate/patrolTemplateList',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    toDel(row) {
      this.axios({
        method: 'post',
        url: 'api-gov/ipqc/template/expand/checkdelete',
        data: {
          id: row.id,
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$confirm('确认删除该模板吗?', '提示', {
              cancelButtonClass: 'btn-custom-cancel',
              type: 'warning',
            })
              .then(() => {
                this.delTemplate(row.id)
              })
              .catch(() => {})
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {})
    },
    delTemplate(id) {
      this.axios({
        method: 'post',
        url: 'api-gov/ipqc/template/expand/delete',
        data: {
          id: id,
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$message({
              type: 'success',
              message: '删除成功',
            })
            this.$refs.pagination.getTableListData()
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {})
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getApi() {
      if (localStorage.getItem('judgeRoleCode') === 'company') {
        this.api = 'api-s/report/export_JC_NC_IPQC_TEMPLATE'
      } else {
        this.api = 'api-s/report/export_JC_CS_IPQC_TEMPLATE'
      }
    },
  },
}
</script>

<style scoped lang="less">
.mainCon {
  top: 198px;
  // padding: 24px;
}
@import '../../../static/css/filterArea.less';
</style>
